﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace TiDiSun
{
    public partial class Update_SanPham_MachSac : Form
    {
        public Update_SanPham_MachSac()
        {
            InitializeComponent();
        }

        public int id { get; set; }

        private void Update_SanPham_MachSac_Load(object sender, EventArgs e)
        {
            try
            {
                id = int.Parse(this.Tag.ToString());
                SanPham_MachSacDTO mSac = new SanPham_MachSacDTO();

                // load mach sac theo id
                mSac = MachSacBUS.load_machsac_id(id);
                txtMachSac_HieuSuat1.Text = mSac.hieusuat1;
                txtMachSac_HieuSuat2.Text = mSac.hieusuat2;
                txtMachSac_Ample.Text = mSac.ample;
                txtMachSac_GiaTien.Text = mSac.giatien.ToString();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }



        private bool SanPham_StatusThemMachSac()
        {
            if (txtMachSac_HieuSuat1.Text == "Hãy nhập")
            {
                txtMachSac_HieuSuat1.ForeColor = System.Drawing.Color.Red;
                return false;
            }
            if (txtMachSac_HieuSuat2.Text == "Hãy nhập")
            {
                txtMachSac_HieuSuat2.ForeColor = System.Drawing.Color.Red;
                return false;
            }
            if (txtMachSac_Ample.Text == "Hãy nhập")
            {
                txtMachSac_Ample.ForeColor = System.Drawing.Color.Red;
                return false;
            }
            if (txtMachSac_GiaTien.Text == "Hãy nhập")
            {
                txtMachSac_GiaTien.ForeColor = System.Drawing.Color.Red;
                return false;
            }

            return true;
        }

        private void btnThemMachSac_Click(object sender, EventArgs e)
        {
            if (SanPham_StatusThemMachSac())
            {
                try
                {
                    SanPham_MachSacDTO mSac = new SanPham_MachSacDTO();
                    mSac.id = id;
                    mSac.hieusuat1 = txtMachSac_HieuSuat1.Text;
                    mSac.hieusuat2 = txtMachSac_HieuSuat2.Text;
                    mSac.ample = txtMachSac_Ample.Text;
                    mSac.giatien = int.Parse(txtMachSac_GiaTien.Text);

                    MachSacBUS.Update_sanpham_machsac(mSac);
                    this.Close();
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
        }

        private void txtMachSac_GiaTien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
        }


        // event text changed-------------------------------------------------------------------
        private void txtMachSac_HieuSuat1_TextChanged(object sender, EventArgs e)
        {
            txtMachSac_HieuSuat1.ForeColor = System.Drawing.Color.Black;
        }

        private void txtMachSac_HieuSuat2_TextChanged(object sender, EventArgs e)
        {
            txtMachSac_HieuSuat2.ForeColor = System.Drawing.Color.Black;
        }

        private void txtMachSac_Ample_TextChanged(object sender, EventArgs e)
        {
            txtMachSac_Ample.ForeColor = System.Drawing.Color.Black;
        }

        private void txtMachSac_GiaTien_TextChanged(object sender, EventArgs e)
        {
            txtMachSac_GiaTien.ForeColor = System.Drawing.Color.Black;
        }


        // event click-----------------------------------------------------------------------
        private void txtMachSac_HieuSuat1_Click(object sender, EventArgs e)
        {
            if (txtMachSac_HieuSuat1.Text == "Hãy nhập")
            {
                txtMachSac_HieuSuat1.Text = "";
                txtMachSac_HieuSuat1.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void txtMachSac_HieuSuat2_Click(object sender, EventArgs e)
        {
            if (txtMachSac_HieuSuat2.Text == "Hãy nhập")
            {
                txtMachSac_HieuSuat2.Text = "";
                txtMachSac_HieuSuat2.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void txtMachSac_Ample_Click(object sender, EventArgs e)
        {
            if (txtMachSac_Ample.Text == "Hãy nhập")
            {
                txtMachSac_Ample.Text = "";
                txtMachSac_Ample.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void txtMachSac_GiaTien_Click(object sender, EventArgs e)
        {
            if (txtMachSac_GiaTien.Text == "Hãy nhập")
            {
                txtMachSac_GiaTien.Text = "";
                txtMachSac_GiaTien.ForeColor = System.Drawing.Color.Black;
            }
        }


        // event leave---------------------------------------------------------------------------
        private void txtMachSac_HieuSuat1_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtMachSac_HieuSuat1.Text))
            {
                txtMachSac_HieuSuat1.Text = "Hãy nhập";
                txtMachSac_HieuSuat1.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtMachSac_HieuSuat2_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtMachSac_HieuSuat2.Text))
            {
                txtMachSac_HieuSuat2.Text = "Hãy nhập";
                txtMachSac_HieuSuat2.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtMachSac_Ample_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtMachSac_Ample.Text))
            {
                txtMachSac_Ample.Text = "Hãy nhập";
                txtMachSac_Ample.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtMachSac_GiaTien_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtMachSac_GiaTien.Text))
            {
                txtMachSac_GiaTien.Text = "Hãy nhập";
                txtMachSac_GiaTien.ForeColor = System.Drawing.Color.DarkGray;
            }
        }











    }
}
